package com.finger.crowd.service.api.impl;

import com.finger.crowd.entity.po.AddressPO;
import com.finger.crowd.entity.po.OrderPO;
import com.finger.crowd.entity.po.OrderProjectPO;
import com.finger.crowd.entity.vo.AddressVO;
import com.finger.crowd.entity.vo.OrderProjectVO;
import com.finger.crowd.entity.vo.OrderVO;
import com.finger.crowd.mapper.AddressPOMapper;
import com.finger.crowd.mapper.OrderPOMapper;
import com.finger.crowd.mapper.OrderProjectPOMapper;
import com.finger.crowd.service.api.OrderService;
import com.netflix.discovery.converters.Auto;
import org.apache.tomcat.jni.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderPOMapper orderPOMapper;

    @Autowired
    private AddressPOMapper addressPOMapper;

    @Autowired
    private OrderProjectPOMapper orderProjectPOMapper;


    public OrderProjectVO getOrderProjectVOByReturnId(Integer returnId) {
        return orderProjectPOMapper.getOrderProjectVOByReturnId(returnId);
    }

    public List<AddressVO> getAddressByMemberId(Integer memberId) {
        List<AddressPO> addressPOList = addressPOMapper.selectAddressByMemberId(memberId);

        List<AddressVO> addressVOList = new ArrayList<AddressVO>();

        // 讲 List<AddressPO> 转成 List<AddressVO>
        for (AddressPO addressPO : addressPOList) {

            AddressVO addressVO = new AddressVO();

            BeanUtils.copyProperties(addressPO, addressVO);

            addressVOList.add(addressVO);
        }

        return addressVOList;
    }

    public void saveAddress(AddressVO addressVO) {
        AddressPO addressPO = new AddressPO();

        BeanUtils.copyProperties(addressVO, addressPO);

        addressPOMapper.insertSelective(addressPO);
    }

    public void saveOrderVO(OrderVO orderVO) {

        OrderPO orderPO = new OrderPO();

        BeanUtils.copyProperties(orderVO, orderPO);

        orderPOMapper.insert(orderPO);

        OrderProjectVO orderProjectVO = orderVO.getOrderProjectVO();

        OrderProjectPO orderProjectPO = new OrderProjectPO();

        BeanUtils.copyProperties(orderProjectVO, orderProjectPO);

        Integer orderId = orderPO.getId();

        orderProjectPO.setOrderId(orderId);

        orderProjectPOMapper.insertSelective(orderProjectPO);

    }
}
